var WindDataTables = new Object();

WindDataTables.createConfig = function() {
	var config = new Object();
	
	config.type = "dataTable";
	
	config.tableTitle = "Data tables";
	config.addTableColumns = function(tableModel) {
		{
			var column = new WindTableColumnModel();
			column.setTitle("Code");
			tableModel.addColumn(column);
		}
		{
			var column = new WindTableColumnModel();
			column.setTitle("Title");
			tableModel.addColumn(column);
		}
	}
	config.addTableRowCellsFromEntity = function(row, entity) {
		row.addCell(entity.code);
		row.addCell(entity.title);
		row.addCell(entity.dataType);
	}
	config.dialogInsertTitle = "Insert a new data table";
	config.dialogUpdateTitle = "Update an existing data table";
	config.configureDialogView = function(dialogView, entity) {
		
		var layout = WindLayout.createView();
		//layout.setWidth("100%");
		dialogView.getBody().addView(layout);
		
		var labelColumnWidth = "50px";
		
		{
			var row = layout.createRowView();
			
			{
				var label = WindLabel.createView();
				label.setText("Code:"); 
				var cell = row.createCellView();
				cell.setWidth(labelColumnWidth);
				cell.addView(label);
			}
			
			dialogView.codeText = WindText.createView();
			if(entity!=null) {
				dialogView.codeText.setValue(entity.code);
			} else {
				dialogView.codeText.setValue("TABLE_X");
			}
			var cell = row.createCellView();
			cell.addView(dialogView.codeText);
		}
		{
			var row = layout.createRowView();
			
			{
				var label = WindLabel.createView();
				label.setText("Title:"); 
				var cell = row.createCellView();
				cell.addView(label);
			}
			
			dialogView.titleText = WindText.createView();
			if(entity!=null) {
				dialogView.titleText.setValue(entity.title);
			} else {
				dialogView.titleText.setValue("Table X");
			}
			var cell = row.createCellView();
			cell.addView(dialogView.titleText);
		}
		{
			var row = layout.createRowView();
			
			{
				var label = WindLabel.createView();
				label.setText("Type:"); 
				var cell = row.createCellView();
				cell.setWidth(labelColumnWidth);
				cell.addView(label);
			}
			
			dialogView.typeText = WindText.createView();
			if(entity!=null) {
				dialogView.typeText.setValue(entity.dataType);
			} else {
				dialogView.typeText.setValue("TYPE");
			}
			var cell = row.createCellView();
			cell.addView(dialogView.typeText);
		}
		if(entity!=null) {
			var row = layout.createRowView();
			
			{
				var cell = row.createCellView();
				cell.setColSpan(2);
				
				var config = WindDataTableColumns.createConfig(entity);
				var view = WindEntities.createTableViewFromConfig(config);
				cell.addView(view);
			}
		}
	}
	config.fillEntityFromDialog = function(dialogView, entity) {
		entity.code = dialogView.codeText.getValue();
		entity.title = dialogView.titleText.getValue();
		entity.dataType = dialogView.typeText.getValue();
	}
	
	return config;
}

WindDataTables.createConfigByCode = function(dataTableCode) {
	var config = new Object();

	var cmd = WindData.createCommand("datastore.list", "dataTable", null, "code", "=", dataTableCode);
	var result = WindData.execute(cmd);	
	if(result.errors.length>0) {
		var view = WindMessages.createView();
		for(var i=0; i<result.errors.length; i++) {
			var error = result.errors[i];
			view.addError(error);
		}
		
		var dialogModel = WindDialog.createModel();
		dialogModel.setTitle("Errors");
		
		var button = dialogModel.getToolbar().createButton();
		button.setTitle("Close");
		button.onClick = function() {
			dialogModel.close();
		}

		var dialogView = WindDialog.createView(dialogModel);
		
		dialogView.getBody().addView(view);
		
		dialogView.open();
	} else {
		if(result.rows.length==0) {
			
			var view = WindMessages.createView();
			
			var error = new Object();
			error.text = "Data table not found: " + dataTableCode;
			view.addError(error);
			
			var dialogModel = WindDialog.createModel();
			dialogModel.setTitle("Errors");
			
			var button = dialogModel.getToolbar().createButton();
			button.setTitle("Close");
			button.onClick = function() {
				dialogModel.close();
			}

			var dialogView = WindDialog.createView(dialogModel);
			
			dialogView.getBody().addView(view);
			
			dialogView.open();
			
		} else {
			var dataTable = result.rows[0];
			
			config.type = dataTable.kind;
			
			config.tableTitle = dataTable.title;
			
			config.addTableColumns = function(tableModel) {
	
				var cmd = WindData.createCommand("datastore.list", "dataTableColumn", null, "dataTableCode", "=", dataTableCode);
				var result = WindData.execute(cmd);	
				
				for(var i=0; i<result.rows.length; i++) {
					var dataTableColumn = result.rows[i];
					var column = new WindTableColumnModel();
					column.setTitle(dataTableColumn.title);
					tableModel.addColumn(column);
				}
			}
			
		}
	}

	return config;
}
